using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using System.ComponentModel;
using Microsoft.Xna.Framework.Graphics;
//using SynapseGaming.LightingSystem.Core;

namespace The_LarX
{
    public class Camera3D : Component, I3DCamera
    {
        public Actor Actor;

        public Matrix View { get; set; }
        public Matrix Projection { get; set; }

        public virtual Vector3 Position
        {
            get { return Actor.Position; }
            set { Actor.Position = value; }
        }

        public Vector3 Target { get; set; }

        public float NearPlane { get; set; }
        public float FarPlane
        {
            get { return farPlane; }
            set
            {
                if (value > NearPlane && value > 10)
                {
                    farPlane = value;
                }
            }
        }
        private float farPlane;

        public List<String> ExcludedActors;

        public float AspectRatio { get; set; }

        public Renderer Renderer;

        public Camera3D(Renderer Renderer, GameScreen Screen)
            : base(Screen)
        {
            Load();
        }
        public Camera3D(GameScreen Screen)
            : base(Screen)
        {
            Load();
        }
        public Camera3D()
            : base()
        {
            Load();
        }
        public void Load()
        {
            ExcludedActors = new List<string>();

            this.FarPlane = 100;
            this.NearPlane = 1;

            EngineSystem.Cameras.Add(this);

            Actor = new Actor("Engine\\Cameras\\Models\\model_Camera");
            if (Engine.Editor)
            {
                Actor.SubActors[0].RemapMesh();
                Actor.SubActors[0].DiffuseColor = Color.Yellow.ToVector4();
                Actor.SubActors[0].EmissiveColor = new Vector4(1);
            }

            Actor.Position = new Vector3(0, 5, 0);
            Actor.Parent = this;

            if (Renderer != null)
            {
                Renderer.SubmitActor(Actor);
                Actor.AddActorToRenderer = Renderer.SubmitActor;
                Actor.RemoveActorFromRenderer = Renderer.RemoveActor;
                Actor.Visible = Engine.Editor;
            }            
        }

        public override void Update()
        {
            base.Update();
            
            Actor.Update();

            Actor.Visible = !Engine.PlayMode;
        }
        public override void Draw()
        {
            base.Draw();

            Actor.Draw();                
        }

        public override void SerializeData()
        {
            base.SerializeData();

            Actor.SerializeData(Data);
            Data.AddData("Renderer.Name", Renderer.Name);

            Data.AddData("Camera.Target", this.Target);
            Data.AddData("Camera.NearPlane", this.NearPlane);
            Data.AddData("Camera.FarPlane", this.FarPlane);
            Data.AddData("Camera.AspectRatio", this.AspectRatio);
        }
        public override void DeserializeData()
        {
            base.DeserializeData();

            this.Target = Data.GetData<Vector3>("Camera.Target");
            this.NearPlane = Data.GetData<float>("Camera.NearPlane");
            this.FarPlane = Data.GetData<float>("Camera.FarPlane");
            this.AspectRatio = Data.GetData<float>("Camera.AspectRatio");

            Actor.DeserializeData(Data);
            Renderer = EngineSystem.GetRenderer(Data.GetData<string>("Renderer.Name"));

            if (Renderer != null)
            {
                Renderer.SubmitActor(Actor);

                Actor.AddActorToRenderer = Renderer.SubmitActor;
                Actor.RemoveActorFromRenderer = Renderer.RemoveActor;
                Actor.Visible = Engine.Editor;
            }            
        }
    }
}